package com.momochen.alarmclockdemo.db;


import com.momochen.alarmclockdemo.AppContext;
import com.momochen.alarmclockdemo.bean.AlarmBean;

import org.greenrobot.greendao.query.LazyList;

import java.util.List;

/**
 * Created by momochen on 2016/10/10.
 */

public class AlarmBeanDaoHelper {
    private static AlarmBeanDao alarmBeanDao = AppContext.getInstance().getDaoSession().getAlarmBeanDao();

    public static final AlarmBean queryById(long id) {
        return alarmBeanDao.load(id);
    }

    /**
     * 查找全部
     *
     * @return
     */
    public static final List<AlarmBean> queryAll() {
        AppContext.getInstance().getDaoSession().clear();

        return alarmBeanDao.loadAll();
    }

    /**
     * 查找全部
     *
     * @return
     */
    public static final LazyList<AlarmBean> queryAllByEnabled() {
        AppContext.getInstance().getDaoSession().clear();

        return alarmBeanDao.queryBuilder().where(AlarmBeanDao.Properties.Enabled.eq(1)).listLazy();
    }

    public static final long saveOrUpdateAlarm(AlarmBean alarmBean) {
        return alarmBeanDao.insertOrReplace(alarmBean);
    }

    public static final void deleAlarm(long id) {
        alarmBeanDao.deleteByKey(id);
    }

    public static final void updateAlarm(long id, int enabled, long time) {
        String str = time == -1 ? "" : ("," + AlarmBeanDao.Properties.TimeStamp.columnName + "=" + time);
        alarmBeanDao.getDatabase().execSQL("UPDATE  " + AlarmBeanDao.TABLENAME + " SET " +
                        AlarmBeanDao.Properties.Enabled.columnName + "=" + enabled +
                        str +
                        "  where " + AlarmBeanDao.Properties.Id.columnName + "=?",
                new Object[]{id});
    }
}
